package com.example.yk_lx.ui;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.alibaba.android.arouter.launcher.ARouter;

public class WelcomeView extends View {
    //画笔//倒计时的时间//绘制圆弧时的度数
    private Paint paint;
    private int time;
    private int size;

    public WelcomeView(Context context) {
        super(context);
        initPaint();//初始化画笔
    }

    public WelcomeView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initPaint();//初始化画笔
    }

    public WelcomeView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPaint();//初始化画笔
    }

    public WelcomeView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initPaint();//初始化画笔
    }

    private void initPaint() {
        paint = new Paint();
        paint.setColor(Color.RED);
        paint.setAntiAlias(true);//抗锯齿，----让绘制的图形更圆润
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(5);
    }
    //执行绘画
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        RectF f = new RectF(100, 100, 200, 200);
        canvas.drawArc(f,0,size,false,paint);

        paint.setTextSize(30);//文字大小
        canvas.drawText(time+"",150,150,paint);
    }
    //index---外面设置的倒计时的总时长
    public void start(int index){
        time = index;
        //修改圆弧的值
        {
            ValueAnimator animator = ValueAnimator.ofInt(0, 360);
            animator.setDuration(index*1000);
            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(@NonNull ValueAnimator animation) {
                    size = (int) animation.getAnimatedValue();//更新为弧度的值
                    //重新绘制
                    invalidate();
                }
            });
            animator.start();
        }
        //修改时间的值
        {
            ValueAnimator animator = ValueAnimator.ofInt(index, 0);
            animator.setDuration(index*1000);
            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(@NonNull ValueAnimator animation) {
                    time = (int) animation.getAnimatedValue();
                    invalidate();
                    if(time==0){
                        ARouter.getInstance().build("/app/LoginActivity").navigation();
                    }
                }
            });
            animator.start();

        }

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}
